<?php
	$account_name 			= ( isset($_REQUEST['account_name']) ? $_REQUEST['account_name'] : ''); 
	$account_pass 			= ( isset($_REQUEST['account_pass']) ? $_REQUEST['account_pass'] : ''); 
	$account_pass_retype 	= ( isset($_REQUEST['account_pass_retype']) ? $_REQUEST['account_pass_retype'] : ''); 
	$account_email 			= ( isset($_REQUEST['account_email']) ? $_REQUEST['account_email'] : ''); 
	$account_email_retype 	= ( isset($_REQUEST['account_email_retype']) ? $_REQUEST['account_email_retype'] : ''); 
	$groom_name 			= ( isset($_REQUEST['groom_name']) ? $_REQUEST['groom_name'] : ''); 
	$bride_name 			= ( isset($_REQUEST['bride_name']) ? $_REQUEST['bride_name'] : ''); 
	$wedding_date 			= ( isset($_REQUEST['wedding_date']) ? $_REQUEST['wedding_date'] : ''); 
	$wedding_location 		= ( isset($_REQUEST['wedding_location']) ? $_REQUEST['wedding_location'] : '');

	$_SESSION['error_register'] = NULL;
	if (!$account_name){
		$_SESSION['error_register'] .= _ERROR_MSG_ACCOUNT_NAME_NOT_EMPTY.'<br/>';
		redir(URL."dang-ky");
	}
	
	if ( isAccountNameExists($account_name) ){
		$_SESSION['error_register'] .= _ERROR_MSG_ACCOUNT_NAME_DUPLICATE.'<br/>';
		redir(URL."dang-ky");
	}

	if (!$account_pass){
		$_SESSION['error_register'] .= _ERROR_MSG_ACCOUNT_PASS_NOT_EMPTY.'<br/>';
		redir(URL."dang-ky");
	}
	
	if (!$account_email){
		$_SESSION['error_register'] .= _ERROR_MSG_ACCOUNT_EMAIL_NOT_VALID.'<br/>';
		redir(URL."dang-ky");
	}
	
	if (!$groom_name){
		$_SESSION['error_register'] .= _ERROR_MSG_GROOM_NAME_NOT_EMPTY.'<br/>';
		redir(URL."dang-ky");
	}

	if (!$bride_name){
		$_SESSION['error_register'] .= _ERROR_MSG_BRIDE_NAME_NOT_EMPTY.'<br/>';
		redir(URL."dang-ky");
	}

	if ( $wedding_date != '' ){
		$wedding_date = convertDatetoSQLDate($wedding_date);
	}
	
	# was there a reCAPTCHA response?
	if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer (PRIVATE_KEY,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);

        if (!$resp->is_valid) {
            # set the error code so that we can display it
            $_SESSION['error_register'] .= _ERROR_MSG_RECAPTCHA_WRONG;
            redir(URL."dang-ky");
        }
	}

	if ( sizeof($_SESSION['error_register']) ){
		redir(URL."dang-ky");
	}else{
		insertAccount($account_name
			,md5($account_pass)
			,$account_email
			,$bride_name
			,$groom_name
			,$wedding_date
			,$wedding_location
			,USER_ACCOUNT ) ;
		redir(URL."dang-nhap");
	}
?>